package com.co.search.controller;

import cn.hutool.core.net.URLDecoder;
import com.co.common.entity.Result;
import com.co.search.service.VideoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.nio.charset.Charset;
import java.util.HashMap;
import java.util.Map;

/**
 * @author ldl.plus
 * @date 2020年06月30日  15:37
 */
@RestController
@RequestMapping("/search")
public class VideoController {

    @Autowired
    private VideoService videoService;

    @GetMapping("/import/all")
    public Result<?> importAll() {
        videoService.importData();
        return Result.ok();
    }

    @GetMapping("/create/index")
    public Result<?> createIndex() {
        videoService.createIndex();
        return Result.ok();
    }

    @GetMapping("/feiqi")
    public Result<Map<String, Object>> searchByCondition(@RequestParam Map<String, String> searchMap) {
        Map<String, Object> resultMap = videoService.search(searchMap);
        return Result.ok(resultMap);
    }

    @GetMapping
    public Result<Map<String, Object>> searchByCondition(@RequestParam(required = false) String keyword,
                                                         @RequestParam(required = false) String filter,
                                                         @RequestParam(required = false) String uploadDate,
                                                         @RequestParam(required = false) String duration,
                                                         @RequestParam(required = false, defaultValue = "1") String pageNum,
                                                         @RequestParam(required = false, defaultValue = "28") String pageSize) {
        HashMap<String, String> searchMap = new HashMap<>();
        searchMap.put("keyword", keyword);
        if (keyword.contains("%")){
            keyword = URLDecoder.decode(keyword, Charset.defaultCharset());
            searchMap.put("keyword", keyword);
        }
        searchMap.put("filter", filter);
        searchMap.put("uploadDate", uploadDate);
        searchMap.put("duration", duration);
        searchMap.put("pageNum", pageNum);
        searchMap.put("pageSize", pageSize);
        Map<String, Object> resultMap = videoService.search(searchMap);
        return Result.ok(resultMap);
    }
}
